package com.eeepay.v2_library.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.eeepay.v2_library.R;

/**
 * Created by Ching on 2016/3/3.
 */
public class TwoLineTextView extends RelativeLayout implements View.OnClickListener {

    private TextView topTv, bottomTv, rightTv;
    private Drawable right_drawable;
    private Context mContext;
    private String topText, bottomText;
    private RightBtnOnClickListener rightOnClickListener;

    public TwoLineTextView(Context context) {
        this(context, null);
    }

    public TwoLineTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TwoLineTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TwoLineTextView);
        topText = typedArray.getString(R.styleable.TwoLineTextView_topText);
        bottomText = typedArray.getString(R.styleable.TwoLineTextView_bottomText);

        int rightDrawable_id = typedArray.getResourceId(R.styleable.TwoLineTextView_drawableRight, R.drawable.btn_right_arrow);
        right_drawable = context.getResources().getDrawable(rightDrawable_id);

        View view = LayoutInflater.from(context).inflate(R.layout.view_twoline_textview, this, true);
        topTv = (TextView) view.findViewById(R.id.tv_top);
        bottomTv = (TextView) view.findViewById(R.id.tv_bottom);
        rightTv = (TextView) view.findViewById(R.id.tv_right);

        setTextView();
        typedArray.recycle();

    }

    private void setTextView() {

        if (topText != null) {
            topTv.setText(topText);
        }
        if (right_drawable != null) {
            right_drawable.setBounds(0, 0, right_drawable.getMinimumWidth(), right_drawable.getMinimumHeight());
            rightTv.setCompoundDrawables(null, null, right_drawable, null);
        }
        if (bottomText != null) {
            bottomTv.setText(bottomText);
        }

        rightTv.setOnClickListener(this);
    }

    public void setRightBackgroundResource(int resid) {
        rightTv.setBackgroundResource(resid);
    }

    public void setRightResource(int resid) {
        Drawable drawable = mContext.getResources().getDrawable(resid);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        rightTv.setCompoundDrawables(drawable, null, null, null);
    }

    public void setRightText(String rightText) {
        if (rightTv != null) {
            rightTv.setText(rightText);
        }
    }

    @Override
    public void onClick(View v) {
        if (rightOnClickListener != null) {
            rightOnClickListener.onRightClick(v);
        }
    }

    /**
     * @param rightOnClickListener 设置右边按钮监听
     */
    public void setRightOnClickListener(RightBtnOnClickListener rightOnClickListener) {
        this.rightOnClickListener = rightOnClickListener;
    }

    public interface RightBtnOnClickListener {
        public void onRightClick(View view);
    }
}
